/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UI_Tienda.java
 *
 * Created on Apr 29, 2010, 9:58:12 PM
 */

package UI;

import Excepciones.DinamicaDato;
import Excepciones.ProcesamientoDato;
import Excepciones.Sistema;
import Modelo.BaseDato;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import robot_maker.*;
import robot_maker.ResultSetTableModel;

/**
 *
 * @author CarlosScipione
 */
public class UI_Tienda extends javax.swing.JFrame {
    private int fila;
    private int rowMyor;

    /** Creates new form UI_Tienda */

        public UI_Tienda() {
        initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void addlisteners (){

     this.tienda_table.getSelectionModel().addListSelectionListener(
             new javax.swing.event.ListSelectionListener() {
            @Override
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
               jTable1SelectionChanged(evt);
            }
        });
    }
    public void refrescar() {

try{
            try {
                BaseDato buscar = new BaseDato();
            } catch (CommunicationsException ex) {
                        Sistema.loggear(10, "LLENAR",
                        "COMUNICACION PERDIDA CON LA BASE DE DATOS",
                        UI_Tienda.class, "CommunicationsException");
            } catch (DinamicaDato ex) {
            }
            this.tienda_table.setModel(new ResultSetTableModel("select " +
                "cod \"Codigo\", nombre \"Nombre\", estado \"Estado\" " +
                "from TIENDA"));
            if (this.tienda_table.getRowCount() > 0) {
                this.tienda_table.setRowSelectionInterval(0, 0);
            }
        } catch (ClassNotFoundException ex) {
                    Sistema.loggear(14, "refrescar", "SQL SYNTAXIS",
                            UI_Tienda.class, "ClassNotFoundException");
        } catch (SQLException ex) {
                    Sistema.loggear(14, "refrescar", "SQL SYNTAXIS",
                            UI_Tienda.class, "SQLException");
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tienda_table = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        delete_buton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tiendas");
        setPreferredSize(new java.awt.Dimension(403, 476));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Tiendas:");

        tienda_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tienda_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tienda_tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tienda_table);

        jButton1.setText("+");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        delete_buton.setText("-");
        delete_buton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete_butonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(312, Short.MAX_VALUE)
                .add(delete_buton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(21, 21, 21))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                    .add(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(delete_buton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            try {
                BaseDato buscar = new BaseDato();
            } catch (CommunicationsException ex) {
                        Sistema.loggear(10, "LLENAR",
                        "COMUNICACION PERDIDA CON LA BASE DE DATOS",
                        UI_Clientes.class, "CommunicationsException");
            } catch (DinamicaDato ex) {
            }
            this.tienda_table.setModel(new ResultSetTableModel("select " +
               "cod \"Codigo\", nombre \"Nombre\", estado \"Estado\" " +
               "from TIENDA"));
            if (this.tienda_table.getRowCount() > 0) {
                this.tienda_table.setRowSelectionInterval(0, 0);
            }
        } catch (ClassNotFoundException ex) {
            Sistema.loggear(14, "windowOpen", "SQL SYNTAXIS",
                            UI_Tienda.class, "ClassNotFoundException");
        } catch (SQLException ex) {
            Sistema.loggear(14, "windowOpen", "SQL SYNTAXIS", UI_Tienda.class,
                    "SQLException");        }
    }//GEN-LAST:event_formWindowOpened

    private void tienda_tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tienda_tableMouseClicked
   /*     int i = 0;
        if (evt.getClickCount() == 2) {
            JTable target = (JTable)evt.getSource();
            int row = target.getSelectedRow();
            int column = target.getSelectedColumn();
            ResultSet lista = null;
            try {
                lista = BaseDato.ejecutarSqlSelect("select * from TIENDA");
            } catch (BasesdeDatos ex) {
            }
            while (i <= row){
                try {
                    lista.next();
                    i++;
                } catch (SQLException ex) {
            Sistema.loggear(14, "mouseClicked", "SQL SYNTAXIS", UI_Tienda.class,
                    "SQLException");                }
            }
       
           

            UI_Venta window;
            try {
                window = new UI_Venta(lista.getString(1), lista.getString(2), 
                        lista.getString(3), lista.getString(4),
                        lista.getString(5), lista.getString(6));
                window = (UI_Venta) Manager.centra(window);
                window.setVisible(true);
            } catch (SQLException ex) {
            Sistema.loggear(14, "mouseClicked", "SQL SYNTAXIS", UI_Tienda.class,
                    "SQLException");            }
            
        }*/


         rowMyor = tienda_table.getSelectedRow() + 1;
        if(evt.getClickCount() == 2){
              ResultSet lista = null;
            try {
                lista = BaseDato.ejecutarSqlSelect("select COD from TIENDA");
            } catch (DinamicaDato ex) {
            }
            try {
                int i =1;
                lista.next();
                while(i < rowMyor){
                    lista.next();
                    i++;
                }
            } catch (SQLException ex) {
                        Sistema.loggear(11, "MouseClicked",
                        "EN EL SQL DE EJECUCCION",
                        UI_Tienda.class, "SQLException");            }

        addTienda window = null;
            try {
                window = new addTienda(this, true, lista.getInt(1));
            } catch (SQLException ex) {
                    Sistema.loggear(11, "MouseClicked",
                        "EN EL SQL DE EJECUCION",
                        UI_Tienda.class, "SQLException");            }
        window = (addTienda) Manager.centra(window);
        window.setVisible(true);
        }

    }//GEN-LAST:event_tienda_tableMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        addTienda window = new addTienda(this, false, -1);
        window = (addTienda) Manager.centra(window);
        window.setVisible(true);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void delete_butonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_butonActionPerformed
        Tienda eliminar = new Tienda();
        ResultSet lista = null;
        try {
            lista = BaseDato.ejecutarSqlSelect("select COD from TIENDA");
        } catch (DinamicaDato ex) {
        }
            try {
                int i =1;
                lista.next();
                while(i < rowMyor){
                    lista.next();
                    i++;
                }
            try {
                Object[] options = {"Si",
                    "No",
                    "Cancelar"};
        int n = JOptionPane.showOptionDialog(null,
            "Esto borrara toda la informacion de la TIENDA incluyendo " +
            "compras de robots",
            "¿Esta seguro?",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);
                eliminar.borrarBD(lista.getInt(1), n);
            } catch (ProcesamientoDato ex) {
            }
            } catch (SQLException ex) {
                        Sistema.loggear(11, "DELETEBOTON",
                        "EN EL SQL DE EJECUCION",
                        UI_Clientes.class, "SQLException");             }
         refrescar();
    }//GEN-LAST:event_delete_butonActionPerformed

 private void jTable1SelectionChanged(javax.swing.event.ListSelectionEvent evt){


        if (this.tienda_table.getRowCount()>0 && 
                !this.tienda_table.getSelectionModel().getValueIsAdjusting()
                && this.tienda_table.getSelectedRow()>-1){

            this.fila = tienda_table.getSelectedRow();



                String id =tienda_table.getValueAt(this.fila, 0).toString();



        }
    }



    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UI_Tienda().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton delete_buton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tienda_table;
    // End of variables declaration//GEN-END:variables

}
